﻿var environment = environment ||
    {
        getEnvironmentInfoes: window.prefixURL + "Environment/GetEnvironmentInfoes",
        getEnvironmentInfo: window.prefixURL + "Environment/GetEnvironmentInfo",
        delEnvironmentInfo: window.prefixURL + "Environment/DeleteEnvironmentInfo",
        addOrUpdateEnvInfo: window.prefixURL + "Environment/AddOrUpdateEnvInfo",

        bodyTpl: _.template($("#listTemplate").html()),
        pagerTpl: _.template($("#pagingTablePager").html()),

        pageIndex: function () {
            return $('#nav').find('li[flag]').find('a').html();
        },
        pageSize: function () {
            return $('#nav').find('.pagingTablePageCountSelect').val();
        },

        getList: function (pageIndex) {
            var t = this;
            var url = t.getEnvironmentInfoes + "?pageIndex=" + pageIndex + "&pageSize=10";
            $.ajax({
                url: url,
                type: "GET",
                dataType: "Json",
                success: function (data) {
                    if (data != null) {
                        $("#tbody").html(t.bodyTpl(data.returnObj));
                        $("#nav").html(t.pagerTpl(data.pager));
                        $(".pagingTablePageCountSelect").val(data.pager.PageSize);
                    }
                },
                error: function (data) {

                }
            });
        },

        del: function (sender, id) {
            var returnVal = confirm("Are you sure？");
            if (returnVal === false) {
                return;
            } else {
                $.ajax({
                    url: sender.delEnvironmentInfo + "?id=" + id,
                    type: "GET",
                    dataType: "Json",
                    success: function (data) {
                        if (data.returnObj === true) {
                            alert("Success！");
                            sender.getList(1);
                        } else {
                            alert("Error:" + data.businessMessage);
                        }
                    }
                });
            }
        },

        edit: function (sender, id) {
            $.ajax({
                url: sender.getEnvironmentInfo + "?id=" + id,
                type: "GET",
                dataType: "Json",
                success: function (data) {
                    if (data.businessMessage !== -1) {
                        $("#txtEnvironmentId").val(data.returnObj.id);
                        $("#txtName").val(data.returnObj.name);
                        $("#txtDecription").val(data.returnObj.description);
                        $("#createModel").modal();
                    } else {
                        alert(data.businessMessage);
                    }
                },
                error: function (data) {
                    alert("Error:" + data);
                }
            });
        },

        initPage: function () {
            var t = this;
            t.getList(1);
        },

        bindEvents: function () {
            var t = this;

            $("span[name='opt-create']").bind("click", function () {
                $("#txtEnvironmentId").val("");
                $("#txtName").val("");
                $("#txtDecription").val("");
                $("#createModel").modal();
            });

            $("#tbody").delegate("tr td span", "click", function () {
                var type = $(this).data("type");
                var id = $(this).data("id");
                if (type === "edit") {
                    t.edit(t, id);
                } else {
                    t.del(t, id);
                }
            });

            $("#btnSubmit").bind("click", function () {

	            var $btn = $(this).button("loading...");
                var id = $("#txtEnvironmentId").val();
                var name = $("#txtName").val();
                var description = $("#txtDecription").val();

                $.ajax({
                    url: t.addOrUpdateEnvInfo,
                    type: "POST",
                    dataType: "Json",
                    data: {
                        Id: id,
                        Name: name,
                        Description: description
                    },
                    success: function (data) {
                        if (data.returnObj === true) {
                            $("#createModel").modal('hide');
	                        $btn.button('reset');
                            t.getList(1);
                        } else {
	                        $("#createErrorMsg").show().children("div").find("span").html(data.businessMessage);
	                        $btn.button('reset');
                        }
                    },
                    error: function (event) {
                        alert("Error:" + event);
                    }
                });
            });

	        $("#nav").delegate("ul li:not('.disabled')", 'click', function () {
		        var arialabel = $(this).children('a').attr('aria-label');
		        var pageIndex = $(this).children('a').attr('data-page');
		        var activePageIndex = t.pageIndex();
		        if (arialabel != undefined) {
			        if (arialabel === 'Previous') {
				        t.getList(parseInt(activePageIndex) - 1);
			        } else if (arialabel === 'Next') {
				        t.getList(parseInt(activePageIndex) + 1);
			        }
		        } else {
			        t.getList(pageIndex);
		        }
	        });
        },

        //@入口方法
        init: function () {
            var t = this;
            t.bindEvents();
            t.initPage();
        }
    }.init();